//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    img_l: '../images/image44.png',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    hasLogin: wx.getStorageSync("token") ? true : false,
    showSetting: false,
    data:{},
    goodsList:[],//产品数据列表
    goodsType:0,
    pid:"",
    TypeId:'',
    TypeName:'',
    arrName:'',
    overall:{},
    status:false
  },
  // 编辑店铺
  edit: function () {
    wx.navigateTo({
      url: '../stationModify1/stationModify1'
    })
  },
  logs(e){
    let number = e.currentTarget.id;
    if (number==0){
      wx.showModal({
        title: '提示',
        content: '当前暂无访问量',
      })
      return;
    }else{
      wx.navigateTo({
        url: '../logs/logs'
      })
    }
    
  },
  // 获取用户信息
  getUserInfo(res){
    console.log(res.detail.userInfo)
    let that = this;
    wx.setStorageSync("userName", res.detail.rawData);
    var userName = res.detail.userInfo;
    if (res.detail.userInfo) {
      wx.login({
        success: function (res) {
          let code = res.code;
          console.log(code)
          app.post("/login/check","post",{}).then(data =>{
            if (data.statusCode == "302") {
              wx.navigateTo({
                url: '/pages/login/login?code=' + code + "&nickName=" + userName.nickName + "&avatarUrl=" + userName.avatarUrl
              }) 
            } else {
              wx.setStorageSync("token", data.token);
              that.setData({ hasLogin: true });
              wx.showToast({
                title: '登录成功'
              })
            }
          },err =>{
            console.log(err)
          })
          

          
        }
      })
     
    }else{
      //不允许登录
      wx.showToast({ title: '请再次点击授权允许', icon: 'none', duration: 1500 })
      that.setData({ showSetting: true })
      // wx.navigateTo({
      //   url: '/pages/login/login'
      // }) 
    }
  },
  //设置回调
  settingBack: function (res) {
    let that = this;
    if (res.detail.authSetting['scope.userLocation']) {
      that.setData({ showSetting: false });
      that.location();
    }
    if (res.detail.authSetting['scope.userInfo']) {
      that.setData({ showSetting: false });
    }
  },
  // 点击编辑产品
  goodsDetailClick(e){
    console.log(e)
    var data = e.currentTarget.dataset.data;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../chanpinDetail1/chanpinDetail1?data=' + JSON.stringify(data) + "&id=" + id,
    })
  },
  jiayouDetailClick(e) {
    console.log(e)
    var data = e.currentTarget.dataset.data;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../chanpinDetail/chanpinDetail?data=' + JSON.stringify(data)+"&id="+id,
    })
  },
  // 添加产品
  add: function (e) {
    var id = e.currentTarget.id;
    console.log(e)
    if(id!=0){
      wx.navigateTo({
        url: '../EditProductInfo/EditProductInfo?status=' + id
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请选择经营类型',
      })
    }
   
  },
  adds: function (e) {
    var id = e.currentTarget.id;
    if (id != 0) {
      wx.navigateTo({
        url: '../EditProductInfo1/EditProductInfo1?status=' + id
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请选择经营类型',
      })
    }
  },
  //事件处理函数
  gojh:function(){
    wx.navigateTo({
      url: '../index1/index1'
    })
  },
  // 已保存的
  gojh1: function () {
    let that = this;
    wx.navigateTo({
      url: '../index3/index3?pid=' + that.data.pid + "&TypeId=" + that.data.TypeId + "&TypeName=" + that.data.TypeName + "&arrname=" + that.data.arrName,
    })
  },
  onLoad: function () {
    let that = this;
 
  },
  chooseImg: function() {//调用
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        _this.setData({
          img_l: res.tempFilePaths
        })
      }
    })
  },
  onShow: function () {
    let that = this;
    that.setData({ hasLogin: wx.getStorageSync("token") ? true : false });
    app.post("/merchant/visitor_count", "get", {}).then(res => {
      that.setData({ overall: res })
    })
    // 
    app.post("/index/merchant_info", "get", {}).then(res => {
      console.log(res)
      if (res.statusCode == "302"){
        wx.showModal({
          title: '提示',
          content: '登录已过期请重新登录', 
          success: function (res) {
            that.setData({ hasLogin: false });
            wx.removeStorage({
              key:'token'
            });
            // if (res.cancel) {
            //   wx.removeStorage("token");

            // }else{
            //   wx.navigateTo({
            //     url: '/pages/login/login'
            //   })
            // }
          }
        })
        // wx.navigateTo({
        //   url: '/pages/login/login'
        // })
        // wx.t({
        //   title: '提示',
        //   content: '登录已过期请重新登录',
        //   success:function(res){
        //     if (res.cancel) {
        //       //点击取消,默认隐藏弹框
        //     } else {
        //       //点击确定
        //       wx.navigateTo({
        //         url: '/pages/login/login'
        //       })
        //     }
            
        //   }
        // })
        
        // wx.showToast({
        //   title: '登录已过期',
        //   icon: 'loading',
        //   duration:1000
        // })
        
      }else{
        that.setData({
          data: res,
          pid: res.merchant.org_type_id,
          TypeId: res.merchant.merchant_type_id
        })
        if (res.merchant.cover_image) {
          that.setData({ img_l: res.merchant.cover_image, })
        }
        if (res.merchant.merchant_type) {
          that.setData({ arrName: res.merchant.merchant_type.name })
        }
        if (res.merchant.org_type_id == 3) {
          that.setData({ TypeName: '餐饮美食' })
        } else if (res.merchant.org_type_id == 4) {
          that.setData({ TypeName: '酒店住宿' })
        } else if (res.merchant.org_type_id == 5) {
          that.setData({ TypeName: '休闲服务' })
        } else if (res.merchant.org_type_id == 6) {
          that.setData({ TypeName: '汽修汽配' })
        } else if (res.merchant.org_type_id == 7) {
          that.setData({ TypeName: '便利杂货' })
        } else if (res.merchant.org_type_id == 8) {
          that.setData({ TypeName: '加油站' })
        }
      }
      
    });
    
    // 产品列表
    that.chanpinList();
  },
  // 点击点评
  commentClick(e){
    let number = e.currentTarget.id;
    if (number == 0) {
      wx.showModal({
        title: '提示',
        content: '当前暂无访问量',
      })
      return;
    } else {
      wx.navigateTo({
        url: '../comment/comment'
      })
    }
    
  },
  // 点击优惠券
  manage(){
    wx.navigateTo({
      url: '../manageCardAll/manageCardAll'
    })
  },
  input: function () {
    wx.navigateTo({
      url: '../HXInputCode/HXInputCode'
    })
  },
  scan: function () {
    wx.navigateTo({
      url: '../ScranRecord/ScranRecord'
    })
  },
  // 上架
  putClick(e){
    let that = this;
    var id = e.currentTarget.id;
    
    wx.showModal({
      title: '提示',
      content: '确定要产品上/下架吗？',
      success:function(res){
        if (res.cancel){
        }else{
          console.log(id)
          that.onSaleList(id)
        }
      }
    })
    
  },
  delClick(e){
    let that = this;
    var id = e.currentTarget.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除产品吗？',
      success: function (res) {
        if (res.cancel) {
        } else {
          app.post("/goods/del","DELETE",{id:id}).then(res =>{
            wx.showToast({
              title: '删除成功',
            })
            that.chanpinList();
          })
        }
      }
    })
  },
  onSaleList(id){
    let that = this;
    app.post("/goods/on_sale","put",{id:id}).then(res =>{
      wx.showToast({
        title: res.msg,
      })
      // 产品列表
      that.chanpinList();
    });
   
  },
  chanpinList(){
    let that = this;
    app.post("/goods/list", "get", {}).then(res => {
      console.log(res)
      if (res.statusCode!=302){
        res.forEach(function (v, i) {
          res[i].time = that.dateFormat(res[i].update_time, 'yyyy-MM-dd hh:mm')
        })
        that.setData({ goodsList: res, goodsType: res.length })
      }
      
    })
  },
  dateFormat: function (date, format) {
    date = new Date(date * 1000);
    var map = {
      "M": date.getMonth() + 1, //月份
      "d": date.getDate(), //日
      "h": date.getHours(), //小时
      "m": date.getMinutes(), //分
      "s": date.getSeconds(), //秒
      "q": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
      var v = map[t];
      if (v !== undefined) {
        if (all.length > 1) {
          v = '0' + v;
          v = v.substr(v.length - 2);
        }
        return v;
      } else if (t === 'y') {
        return (date.getFullYear() + '').substr(4 - all.length);
      }
      return all;
    });
    return format;

  }
})
